package com.csw;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

import java.io.IOException;

public class Demo01Test {
    public static void main(String[] args) throws IOException {
        //创建配置
        Configuration conf = HBaseConfiguration.create();
        //设置hbase  zookeeper连接地址
        conf.set("hbase.zookeeper.quorum","master:2181,node1:2181,node2:2181");
        //可以按需要加入其它配置  可参考http://hbase.apache.org/book.html#config.files

        //创建与hbase的连接
        Connection conn = ConnectionFactory.createConnection(conf);

        Admin admin = conn.getAdmin();
        //构建NamespaceDescriptor对象
        NamespaceDescriptor apiNS = NamespaceDescriptor.create("apiNS").build();
        //通过admin的createNamespace方法，传入NamespaceDescriptor对象，创建Namespace
        admin.createNamespace(apiNS);

        //创建HTableDescriptor对象，设置表的名称
        HTableDescriptor test1 = new HTableDescriptor(TableName.valueOf("apiNS:test1"));

        //创建HColumnDescriptor的对象，设置表的列族名
        HColumnDescriptor csw1 = new HColumnDescriptor("csw1");
        test1.addFamily(csw1);
        //设置列族csw1的versions为1到5
        csw1.setVersions(1,5);
        HColumnDescriptor csw2 = new HColumnDescriptor("csw2");
        test1.addFamily(csw2);
//        //对表做一些配置
//        test1.setMemStoreFlushSize(128);
        //表配置好了，列族配置好了，使用admin的createTable方法，传入HTableDescriptor对象，完成表的创建
        admin.createTable(test1);

        //关闭连接
        conn.close();
    }
}
